<?php

require_once("Model/ModelsInclude.php");

require_once("Layout/header.php");
require_once("Layout/controllersPageLayout.php");

require_once("Lib/Utils.php");

define("ERROR_MSG", "Stampa fallita!");

/*
 * Istanza generica per accesso ad attributi del model.
 */
$modelToInstanziate = $_GET["ModelName"];
$modelInstanceId = $_GET["ID"];
if($modelToInstanziate == "" || $modelInstanceId == "" || !class_exists($modelToInstanziate))
{
	echo("<p style=\"color:#f00;\"><b>Parametri della richiesta non validi!</b></p>");
	die();
}
$c = new $modelToInstanziate();
if(!$c->isPrintAvailable())
{
	echo("<p style=\"color:#f00;\"><b>Parametri della richiesta non validi!</b></p>");
	die();
}

/*
 * Connessione DB.
 */
$db = new DatabaseManager();
$db->Connect();

/*
 * Aggiunta a DB.
 */
$newObj = $c->getFromDB($db,$modelInstanceId);

if($newObj != NULL)
{
	$res = $newObj->printExcelFile($db);
	switch($res)
	{
		case StdModel::PRINT_DB_FAILED: printFailPage(ERROR_MSG, $db->lastQuery); break;
		case StdModel::PRINT_FILE_FAILED: echo("<p style=\"color:#f00;\"><b>Errore creazione file: Si prega di riprovare.</b><br/><br/>Messaggio di errore: ".$newObj->getLastPrintError()."</p>"); break;
		case StdModel::PRINT_SUCCESS: break;
	}
}
else
	printFailPage(ERROR_MSG, $db->lastQuery);

?>
